package com.xteam.youin_supplier.adpter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xteam.youin_supplier.R;
import com.xteam.youin_supplier.model.BeddingListItem;


/**
 * Created by Curry on 2015-12-16.
 */
public class TenderListViewAdapter extends RecyclerView.Adapter<TenderListViewAdapter.ViewHolder> implements View.OnClickListener {

    private Context mContext;
    private BeddingListItem beddingListItem;
    public TenderListViewAdapter(Context context, BeddingListItem beddingListItem) {
        mContext = context;
        this.beddingListItem = beddingListItem;
    }
    private OnRecyclerViewItemClickListener mOnItemClickListener = null;

    @Override
    public void onClick(View view) {
        if (mOnItemClickListener != null) {
            //注意这里使用getTag方法获取数据
            mOnItemClickListener.onItemClick(view,(int)view.getTag());
        }
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    //define interface
    public static interface OnRecyclerViewItemClickListener {
        void onItemClick(View view , int position);
    }

    @Override
    public TenderListViewAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.tender_list_title_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        //将创建的View注册点击事件
        view.setOnClickListener(this);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(TenderListViewAdapter.ViewHolder holder, int position) {
        holder.itemView.setTag(position);
        holder.getAddr().setText(beddingListItem.getData().get(position).getLocation());
        holder.getDatetime().setText(beddingListItem.getData().get(position).getEnd_time());
        holder.getNum().setText(beddingListItem.getData().get(position).getQty());
        holder.getUnit().setText(beddingListItem.getData().get(position).getName());
    }

    @Override
    public int getItemCount() {
        return beddingListItem.getData().size();
    }


    class ViewHolder extends RecyclerView.ViewHolder{
        private TextView addr;
        private TextView datetime;
        private TextView num;
        private TextView unit;
        public ViewHolder(View itemView) {
            super(itemView);
            addr = (TextView) itemView.findViewById(R.id.tender_addr_content);
            datetime = (TextView) itemView.findViewById(R.id.tender_datetime_content);
            num = (TextView) itemView.findViewById(R.id.tender_num_content);
            unit = (TextView) itemView.findViewById(R.id.tender_unit_content);
        }

        public TextView getAddr() {
            return addr;
        }

        public void setAddr(TextView addr) {
            this.addr = addr;
        }

        public TextView getDatetime() {
            return datetime;
        }

        public void setDatetime(TextView datetime) {
            this.datetime = datetime;
        }

        public TextView getNum() {
            return num;
        }

        public void setNum(TextView num) {
            this.num = num;
        }

        public TextView getUnit() {
            return unit;
        }

        public void setUnit(TextView unit) {
            this.unit = unit;
        }
    }
}
